package com.itjin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itjin.model.domain.entity.UserFollow;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserFollowMapper extends BaseMapper<UserFollow> {

  @Update("INSERT INTO user_follow (followerId, followedId, relationType, status) " +
      "VALUES (#{followerId}, #{followedId}, 1, 1) " +
      "ON DUPLICATE KEY UPDATE status = IF(status = 0, 1, 0), " +
      "updateTime = CURRENT_TIMESTAMP")
  int toggleFollow(@Param("followerId") Integer followerId, @Param("followedId") Integer followedId);

  /**
   * 更新用户的粉丝数
   * 
   * @param userId 用户ID
   * @param delta  增减值（1或-1）
   * @return 影响行数
   */
  @Update("UPDATE user SET myFans = myFans + #{delta} WHERE id = #{userId}")
  int updateUserFans(@Param("userId") Integer userId, @Param("delta") Integer delta);

  /**
   * 更新用户的关注数
   * 
   * @param userId 用户ID
   * @param delta  增减值（1或-1）
   * @return 影响行数
   */
  @Update("UPDATE user SET myFollows = myFollows + #{delta} WHERE id = #{userId}")
  int updateUserFollows(@Param("userId") Integer userId, @Param("delta") Integer delta);
}